package org.vaadinpa.data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.vaadinpa.data.ships.Achilles;
import org.vaadinpa.data.ships.Agamemnon;
import org.vaadinpa.data.ships.Ajax;
import org.vaadinpa.data.ships.Hector;
import org.vaadinpa.data.ships.Hecuba;
import org.vaadinpa.data.ships.Helen;
import org.vaadinpa.data.ships.Menelaus;
import org.vaadinpa.data.ships.Odysseys;
import org.vaadinpa.data.ships.Paris;
import org.vaadinpa.data.ships.Priam;

public abstract class Ship implements Cloneable {

	public enum ShipType {
		FIGHTER, FRIGATE, DESTROYER, CRUISER, BATTLESHIP, ASTEROID
	}

	public static List<Ship> getAvailableShips() {
		List<Ship> ships = new ArrayList<Ship>();

		ships.add(new Achilles());
		ships.add(new Agamemnon());
		ships.add(new Ajax());
		ships.add(new Hector());
		ships.add(new Menelaus());
		ships.add(new Odysseys());
		ships.add(new Paris());
		ships.add(new Priam());

		ships.add(new Hecuba());
		ships.add(new Helen());

		Collections.sort(ships, new Comparator<Ship>() {
			public int compare(Ship o1, Ship o2) {
				return o1.getInit() - o2.getInit();
			}
		});

		return ships;
	}

	public abstract ShipType getType();

	public abstract ShipType getPrimaryTarget();

	public abstract ShipType getSecondaryTarget();

	public abstract String getName();

	public abstract String getDescription();

	public abstract int getArmor();

	public abstract int getPower();

	public abstract int getInit();

	public abstract int getPrice(Resource resource);

	public int getTotalPrice() {
		int total = 0;
		for (Resource r : Resource.values()) {
			total += getPrice(r);
		}
		return total;
	}

	public String getArmorToCost() {
		return getToCost(getArmor());
	}

	public String getPowerToCost() {
		return getToCost(getPower());
	}

	private String getToCost(int num) {
		BigDecimal amount = new BigDecimal(num);
		BigDecimal price = new BigDecimal(getTotalPrice());
		amount = amount.divide(price, 10, RoundingMode.HALF_UP).multiply(new BigDecimal(1000));
		return amount.setScale(0, RoundingMode.HALF_UP).toString();
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean equals(Object other) {
		if (other == null) {
			return false;
		}
		return getClass().equals(other.getClass());
	}

	@Override
	public int hashCode() {
		return getClass().hashCode();
	}
}
